package com.dmbjz.webdemo.service;


import com.dmbjz.webdemo.entity.Person;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * 专门处理用户有关的业务，每页业务放一个自己的处理类
 */
@Service
@Slf4j
public class UserService {


    /**
     * 查询用户
     */
    public ServerResponse getUser(ServerRequest request) {

        //获取请求信息
        String userId = request.pathVariable("id");
        log.info("[getUser]-pathVariable 请求参数：" + userId);

        //返回结果
        Person person = new Person();
        person.setEmail("12312@gmail.com");
        person.setUserName("小明" + userId);
        person.setAge(18);
        return ServerResponse.ok().body(person);

    }


    /**
     * 查询用户列表
     */
    public ServerResponse getUserList(ServerRequest request) {

        //返回结果
        List<Person> personList = new ArrayList<>(1);
        for (int i = 0; i < 3; i++) {
            Person person = new Person();
            person.setEmail("10203@gmail.com");
            person.setUserName("小明" + i);
            person.setAge(18);
            personList.add(person);
        }

        return ServerResponse.ok().body(personList);

    }


    /**
     * 保存用户
     */
    public ServerResponse saveUser(ServerRequest request) throws Exception {

        //获取请求时携带的请求体信息
        Person inputPerson = request.body(Person.class);
        log.info("[saveUser]-请求体参数：{}", inputPerson);

        //返回结果
        return ServerResponse.ok().build();

    }


    /**
     * 更新用户
     */
    public ServerResponse updateUser(ServerRequest request) throws Exception {

        //获取请求时携带的 pathVariable、请求体信息

        String userId = request.pathVariable("id");
        log.info("[updateUser]-pathVariable 请求参数：" + userId);

        Person inputPerson = request.body(Person.class);
        log.info("[updateUser]-请求体参数：{}", inputPerson);

        return ServerResponse.ok().body("用户信息更新完成");

    }


    /**
     * 删除用户
     */
    public ServerResponse delUser(ServerRequest request) {

        //获取请求时携带的 pathVariable、请求体信息
        String userId = request.pathVariable("id");
        log.info("[delUser]-pathVariable 请求参数：" + userId);

        return ServerResponse.ok().build();

    }


}
